
package com.chain.qs.common.web;


import com.chain.qs.common.utils.CacheUtils;
import com.chain.qs.common.utils.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.stereotype.Controller;
import java.util.Map;

/**
 * WebsocketController
 */
@Controller
@MessageMapping("ws")
public class WSMassageController {

	/**
	 * 通知后台更新对应sessionId的当前站点信息
	 * @param stationCode
	 * @param headerAccessor
	 * @throws Exception
	 */
	@MessageMapping("notifyBackendUpdateSCode")
	public void notifyBackendUpdateSCode(String stationCode,SimpMessageHeaderAccessor headerAccessor) throws Exception {
		String sessionId = (String)headerAccessor.getMessageHeaders().get("simpSessionId");
		if(StringUtils.isNumeric(stationCode) && sessionId.length() == 8){
			Map<String, String> wsSessionMap = (Map<String, String>)CacheUtils.get("ws_sessions");
			wsSessionMap = (wsSessionMap == null ? Maps.newConcurrentMap() : wsSessionMap);
			wsSessionMap.put(sessionId,stationCode);
		}
	}
}
